package com.sangeng.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.User;
import com.sangeng.service.LoginService;
import com.sangeng.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private RedisCache redisCache;
    //登录
    @PostMapping("/api/login")
    //返回 username password uuid protect(前端输入的验证码)
    public ResponseResult login(User user,@RequestParam("protect") String protect,@RequestParam("uuid") String uuid){

        String decode =redisCache.getCacheObject(uuid);
//
//        System.out.println("账号: "+user.getUserName());
//
//        System.out.println("密码: "+user.getPassword());

        if(protect.equals(decode)){

            //删除redis的验证码
            redisCache.deleteObject(uuid);
            //登录
            System.out.println(user);
            return loginService.login(user);
        }


        //验证码不一样直接返回错误
         return new ResponseResult(200,"验证码错误");



    }
    //退出
    @RequestMapping("/api/logout")
    public ResponseResult logout(){
        return loginService.logout();
    }

    //获取用户的信息
    @GetMapping("/api/info")
    public ResponseResult userInfo(){
//        System.out.println(userName);
    return    loginService.getUserInfo();
    }


    @GetMapping("api/checkToken")
    public ResponseResult checkUser(){
        return null;
    }


    //验证码
    @GetMapping("api/protect")
    public ResponseResult  getprotect(){
          //定义图形验证码的长和宽 发送base64
         return loginService.getprotect();
    }
}
